﻿namespace RPCenter.WindowsAPI
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.Drawing;

    public class NativeIcon
    {
        public static List<Icon> GetAllIcons(string FilePath, bool GetSmallIcons)
        {
            IntPtr ptr3 = IntPtr.Zero;
            uint num = 0;
            IntPtr phiconLarge = ptr3;
            IntPtr phiconSmall = ptr3;
            num = ApiDefinitions.ExtractIconEx(FilePath, -1, ref phiconLarge, ref phiconSmall, 0);
            if (num == 0L)
            {
                throw new ApplicationException("The specified file could not be found or does not contain any icons");
            }
            List<Icon> list2 = new List<Icon>((int) num);
            int num3 = ((int) num) - 1;
            for (int i = 0; i <= num3; i++)
            {
                IntPtr ptr2 = new IntPtr();
                IntPtr ptr = new IntPtr();
                ApiDefinitions.ExtractIconEx(FilePath, i, ref ptr, ref ptr2, 1);
                if (GetSmallIcons)
                {
                    list2.Add(Icon.FromHandle(ptr2));
                }
                else
                {
                    list2.Add(Icon.FromHandle(ptr));
                }
            }
            return list2;
        }

        public static Icon GetIconAtIndex(string FilePath, int Index, bool GetSmallIcon)
        {
            IntPtr ptr3 = IntPtr.Zero;
            uint num = 0;
            IntPtr phiconLarge = ptr3;
            IntPtr phiconSmall = ptr3;
            num = ApiDefinitions.ExtractIconEx(FilePath, -1, ref phiconLarge, ref phiconSmall, 0);
            if (num == 0L)
            {
                throw new ApplicationException("The specified file could not be found or does not contain any icons");
            }
            if (num < (Index + 1))
            {
                throw new ArgumentOutOfRangeException("Index", "The icon index " + Conversions.ToString(Index) + " is not valid as the specified file does not contain this many icons");
            }
            IntPtr ptr2 = new IntPtr();
            IntPtr ptr = new IntPtr();
            ApiDefinitions.ExtractIconEx(FilePath, Index, ref ptr, ref ptr2, 1);
            if (GetSmallIcon)
            {
                return Icon.FromHandle(ptr2);
            }
            return Icon.FromHandle(ptr);
        }

        public static IntPtr GetIconHandleAtIndex(string FilePath, int Index, bool GetSmallIcon)
        {
            IntPtr ptr4 = IntPtr.Zero;
            uint num = 0;
            IntPtr phiconLarge = ptr4;
            IntPtr phiconSmall = ptr4;
            num = ApiDefinitions.ExtractIconEx(FilePath, -1, ref phiconLarge, ref phiconSmall, 0);
            if (num == 0L)
            {
                throw new ApplicationException("The specified file could not be found or does not contain any icons");
            }
            if (num < (Index + 1))
            {
                throw new ArgumentOutOfRangeException("Index", "The icon index " + Conversions.ToString(Index) + " is not valid as the specified file does not contain this many icons");
            }
            IntPtr ptr3 = new IntPtr();
            IntPtr ptr2 = new IntPtr();
            ApiDefinitions.ExtractIconEx(FilePath, Index, ref ptr2, ref ptr3, 1);
            if (GetSmallIcon)
            {
                return ptr3;
            }
            return ptr2;
        }
    }
}

